package com.bc.commonentity.common;

import com.bc.commoncore.util.StringUtils;
import com.bc.commonentity.enums.HttpReturnRnums;
import com.bc.commonentity.log.AspectLogHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import java.util.Locale;

/**
 * Created by Administrator on 2018/5/18.
 */
public class AppGlobalExceptionHandler  implements ExceptionMapper {
    protected static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    @Override
    public Response toResponse(Throwable ex) {
        String stackTrace = StringUtils.getStackTrace(ex);
        logger.error(ex.getMessage()+stackTrace);
        Response.ResponseBuilder rb;
        if(ex instanceof InterceptorNotLoginRunTimeException){
            //app的MsgInterceptor中抛异常时不会调用AppRequestInterceptor.handleMessage，
            //对app来说，即不会把JsonResult类型的返回值转换为AppJsonResult
            //因此这里特殊处理
            AppJsonResult rep=new AppJsonResult();
            rep.setCode(Response.Status.BAD_REQUEST.getStatusCode());
            rep.setMsg(ex.getMessage());
            rep.setContent(null);
            rb = Response.status(Response.Status.BAD_REQUEST);
            rb.entity(rep);
            //app的MsgInterceptor中抛异常时不会调用AppRequestInterceptor.handleMessage，
            // 即不会调用cxfAfterCompletion，导致cat的Transaction无法结束，最终cat调用链混乱
            //因此这里手动调用cxfAfterCompletionOnerror
            AspectLogHelper.cxfAfterCompletionOnerror("");
        }else if(ex instanceof InterceptorRunTimeException){
            //app的MsgInterceptor中抛异常时不会调用AppRequestInterceptor.handleMessage，
            //对app来说，即不会把JsonResult类型的返回值转换为AppJsonResult
            //因此这里特殊处理
            AppJsonResult rep=new AppJsonResult();
            rep.setCode(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            rep.setMsg(ex.getMessage());
            rep.setContent(null);
            rb = Response.status(Response.Status.INTERNAL_SERVER_ERROR);
            rb.entity(rep);
            //app的MsgInterceptor中抛异常时不会调用AppRequestInterceptor.handleMessage，
            // 即不会调用cxfAfterCompletion，导致cat的Transaction无法结束，最终cat调用链混乱
            //因此这里手动调用cxfAfterCompletionOnerror
            AspectLogHelper.cxfAfterCompletionOnerror(ex.getMessage()+stackTrace);
        }else if (ex instanceof NotLoginRunTimeException) {
            //业务接口内部（不包含MsgInterceptor）抛出未登录异常时,
            //这里把code改为400，之后cxf框架会调用AppRequestInterceptor.handleMessage
            JsonResult rep=new JsonResult();
            rep.setCode(Response.Status.BAD_REQUEST.getStatusCode());
            rep.setMsg(ex.getMessage());
            rep.setData(null);

            rb = Response.status(Response.Status.BAD_REQUEST);
            rb.entity(rep);
        }else {
            //业务接口内部抛出异常时
            JsonResult rep=new JsonResult();
            rep.setCode(HttpReturnRnums.SystemError.value());
            rep.setMsg(HttpReturnRnums.SystemError.desc());
            rep.setData(StringUtils.getStackTrace(ex));

            rb = Response.status(Response.Status.INTERNAL_SERVER_ERROR);
            rb.entity(rep);
        }

        rb.type("application/json;charset=UTF-8");
        rb.language(Locale.SIMPLIFIED_CHINESE);
        Response r = rb.build();
        return r;
    }
}
